﻿
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Anno.Repository
{
    public class SqlEntity
    {
        public SqlEntity()
        {
            CommandType = CommandType.Text;

        }
        public SqlEntity(string sql)
        {
            Sql = sql;
            CommandType = CommandType.Text;
        }
        /// <summary>
        /// SQL脚本
        /// </summary>
        public string Sql { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public CommandType CommandType { get; set; }
        public int? CommandTimeout { get; set; }
        /// <summary>
        /// 支持Dapper原生参数
        /// Parameters优先，Parameters ?? Parameter
        /// </summary>
        public object Parameter { get; set; }
        /// <summary>
        /// 入参集合
        /// </summary>
        public List<SqlSugar.SugarParameter> Parameters { get; set; }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="parameterValue"></param>
        /// <param name="direction"></param>
        public void AddParameter(string parameterName, object parameterValue, ParameterDirection direction = ParameterDirection.Input,bool IsRefCursor=false)
        {
            if (Parameters == null) Parameters = new List<SqlSugar.SugarParameter>();

            this.Parameters.Add(new SqlSugar.SugarParameter(parameterName, parameterValue) { Direction = direction, IsRefCursor = IsRefCursor });
        }
        /// <summary>
        /// 批量添加参数
        /// </summary>
        /// <param name="parameters"></param>
        public void AddParameters(params SqlSugar.SugarParameter[] parameters)
        {
            if (parameters?.Length > 0)
            {
                if (Parameters == null) Parameters = new List<SqlSugar.SugarParameter>();
                parameters.ForEach_(param =>
                {
                    this.Parameters.Add(param);
                });
            }
        }
        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="param"></param>
        public void AddParameters(object param)
        {
            this.Parameter = param;
        }

        public static System.Data.DbType GetDataType(Type type)
        {
            if (type == UtilConstants.ByteArrayType)
            {
                return System.Data.DbType.Binary;
            }
            else if (type == UtilConstants.GuidType)
            {
                return System.Data.DbType.Guid;
            }
            else if (type == UtilConstants.IntType)
            {
                return System.Data.DbType.Int32;
            }
            else if (type == UtilConstants.ShortType)
            {
                return System.Data.DbType.Int16;
            }
            else if (type == UtilConstants.LongType)
            {
                return System.Data.DbType.Int64;
            }
            else if (type == UtilConstants.DateType)
            {
                return System.Data.DbType.DateTime;
            }
            else if (type == UtilConstants.DobType)
            {
                return System.Data.DbType.Double;
            }
            else if (type == UtilConstants.DecType)
            {
                return System.Data.DbType.Decimal;
            }
            else if (type == UtilConstants.ByteType)
            {
                return System.Data.DbType.Byte;
            }
            else if (type == UtilConstants.FloatType)
            {
                return System.Data.DbType.Single;
            }
            else if (type == UtilConstants.BoolType)
            {
                return System.Data.DbType.Boolean;
            }
            else if (type == UtilConstants.StringType)
            {
                return System.Data.DbType.String;
            }
            else if (type == UtilConstants.DateTimeOffsetType)
            {
                return System.Data.DbType.DateTimeOffset;
            }
            else if (type == UtilConstants.TimeSpanType)
            {
                return System.Data.DbType.Time;
            }
            else if (type?.Name == "Geometry")
            {
               return System.Data.DbType.Object;
            }
            else if (type != null && type.IsEnum())
            {
                return System.Data.DbType.Int64;
            }
            else if (type == UtilConstants.UIntType)
            {
                return System.Data.DbType.UInt32;
            }
            else if (type == UtilConstants.ULongType)
            {
                return System.Data.DbType.UInt64;
            }
            else if (type == UtilConstants.ShortType)
            {
                return System.Data.DbType.UInt16;
            }
            return System.Data.DbType.String;
        }
    }
    internal static class UtilConstants
    {
        public const string Dot = ".";

        public const char DotChar = '.';

        internal const string Space = " ";

        internal const char SpaceChar = ' ';

        internal const string AssemblyName = "SqlSugar";

        internal const string ReplaceKey = "{662E689B-17A1-4D06-9D27-F29EAB8BC3D6}";

        internal const string ReplaceCommaKey = "{112A689B-17A1-4A06-9D27-A39EAB8BC3D5}";

        internal static Type UShortType = typeof(ushort);

        internal static Type ULongType = typeof(ulong);

        internal static Type UIntType = typeof(uint);

        internal static Type IntType = typeof(int);

        internal static Type LongType = typeof(long);

        internal static Type GuidType = typeof(Guid);

        internal static Type BoolType = typeof(bool);

        internal static Type BoolTypeNull = typeof(bool?);

        internal static Type ByteType = typeof(byte);

        internal static Type ObjType = typeof(object);

        internal static Type DobType = typeof(double);

        internal static Type FloatType = typeof(float);

        internal static Type ShortType = typeof(short);

        internal static Type DecType = typeof(decimal);

        internal static Type StringType = typeof(string);

        internal static Type DateType = typeof(DateTime);

        internal static Type DateTimeOffsetType = typeof(DateTimeOffset);

        internal static Type TimeSpanType = typeof(TimeSpan);

        internal static Type ByteArrayType = typeof(byte[]);

        internal static Type ModelType = typeof(ModelContext);

        internal static Type DynamicType = typeof(System.Dynamic.ExpandoObject);

        internal static Type Dicii = typeof(KeyValuePair<int, int>);

        internal static Type DicIS = typeof(KeyValuePair<int, string>);

        internal static Type DicSi = typeof(KeyValuePair<string, int>);

        internal static Type DicSS = typeof(KeyValuePair<string, string>);

        internal static Type DicOO = typeof(KeyValuePair<object, object>);

        internal static Type DicSo = typeof(KeyValuePair<string, object>);

        internal static Type DicArraySS = typeof(Dictionary<string, string>);

        internal static Type DicArraySO = typeof(Dictionary<string, object>);

        public static Type SugarType = typeof(SqlSugarProvider);

        internal static Type[] NumericalTypes = new Type[8]
        {
            typeof(int),
            typeof(uint),
            typeof(byte),
            typeof(sbyte),
            typeof(long),
            typeof(ulong),
            typeof(short),
            typeof(ushort)
        };

        internal static string[] DateTypeStringList = new string[8] { "Year", "Month", "Day", "Hour", "Second", "Minute", "Millisecond", "Date" };
    }
}
